/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.obu;

import cn.com.taiji.sdk.model.comm.protocol.user.OBUUploadRequest;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.kfwd.OBU;
import com.wttech.dzzf.check.moduleSet.CheckVehicleUpload_9;
import com.wttech.dzzf.process.constant.ExecuteTable;
import com.wttech.dzzf.process.constant.UserConstants;
import com.wttech.dzzf.process.dao.GenerateBlObuInfosDao;
import com.wttech.dzzf.process.dao.HangUpObuInfosDao;
import com.wttech.dzzf.process.model.ObuTemp;
import com.wttech.dzzf.process.model.user.Obu;
import com.wttech.dzzf.process.model.user.User;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import com.wttech.dzzf.process.service.upload.UploadSingleDataService;
import com.wttech.dzzf.process.service.user.UserDetailInfosCreatorService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.wttech.dzzf.process.constant.UserConstants.DEFAULT_VEHICLE_CHAR;
import static com.wttech.dzzf.process.constant.UserConstants.DEFAULT_VEHPLATEID;

/**
 * 挂起OBU信息子服务实现
 *
 * @author MaShibo
 * @date 2017/12/28.
 */
@Service
public class HangUpObuInfoSubService {

	private static final Logger LOGGER = LoggerFactory.getLogger(HangUpObuInfoSubService.class);

	@Resource
	private HangUpObuInfosDao hangUpObuInfosDao;

	@Resource
	private UploadSingleDataService uploadSingleDataService;

	@Resource
	private BaseInfoService baseInfoService;

	@Resource
	private UserDetailInfosCreatorService userDetailInfosCreatorService;


	void processObu(String obuId) {
		OBUUploadRequest obuReq = hangUpObuInfosDao.queryObuByObuId(obuId);
		if (obuReq != null) {
			hangUpObu(obuReq);
		} else {
			OBU obu = hangUpObuInfosDao.queryLastestObuById(obuId);
			String userId = UserConstants.DEFAULT_USERID;
			String vehPlate = obu.getVehiclePlate();
			Integer vehColor = obu.getVehicleColor();
			String cardInfoId = obu.getCardInfoID();
			if (StringUtils.isNotBlank(cardInfoId)) {
				CardInfo cardInfo = hangUpObuInfosDao.queryCardInfoByCardInfoId(cardInfoId);
				if (cardInfo != null && StringUtils.isNotBlank(cardInfo.getUserId())) {
					userId = cardInfo.getUserId();
				}
			} else {
				String vehUserId = hangUpObuInfosDao.queryCardInfoByVeh(vehPlate, vehColor);
				if (StringUtils.isNotBlank(vehUserId)) {
					userId = vehUserId;
				}
			}

			String vehicleId = vehPlate + DEFAULT_VEHICLE_CHAR + vehColor;
			String result = new CheckVehicleUpload_9().checkId(vehicleId);
			String finalVehicleId = baseInfoService.needOutput(result) ?
					DEFAULT_VEHPLATEID : vehicleId;
			User user = new User();
			user.setId(userId);
			Obu obuModel = userDetailInfosCreatorService.buildAndCreateObu(obu, user, finalVehicleId);
			obuReq = new OBUUploadRequest();
			try {
				BeanUtils.copyProperties(obuReq, obuModel);
			} catch (Exception e){
				LOGGER.error(e.getMessage(), e);
			}
			boolean isSuccess = uploadSingleDataService.uploadSdtsInfo(obuReq, ExecuteTable.OBU, true);
			if (isSuccess){
				hangUpObu(obuReq);
			}
		}
	}

	private boolean hangUpObu(OBUUploadRequest obuReq) {
		obuReq.setStatus(3);
		obuReq.setOperation(UserConstants.UPDATE_OPERATION);
		return uploadSingleDataService.uploadSdtsInfo(obuReq, ExecuteTable.OBU, true);
	}


}
